home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Extension Shell 1.5 / Sample Extensions (1.5) / MacCough ƒ / ES Handler.c < prev    next >
C/C++ Source or Header  |  1996-04-12  |  8KB  |  335 lines

  1. /*    NAME:
  2.         ES Handler.c
  3.  
  4.     WRITTEN BY:
  5.         Dair Grant
  6.                 
  7.     DESCRIPTION:
  8.         This file contains an ES Handler for use by Extension Shell.
  9.  
  10.     ___________________________________________________________________________
  11. */
  12. //=============================================================================
  13. //        Include files                                                                     
  14. //-----------------------------------------------------------------------------
  15. #include <Gestalt.h>
  16. #include <Resources.h>
  17. #include <Traps.h>
  18. #include "A4Stuff.h"
  19. #include "SetupA4.h"
  20. #include "ES.h"
  21. #include "MC AddrsTable.h"
  22. #include "MC Constants.h"
  23. #include "ES Handler.h"
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. //=============================================================================
  40. //        Private function prototypes                                                                     
  41. //-----------------------------------------------------------------------------
  42. void    main(short theMsg, ESParamBlock *theParamBlock);
  43. void    InitialiseParamBlock(void);
  44. void    InitialiseAddrsTable(void);
  45. void    HandleTheError(void);
  46. void    SetUpIcons(short animDelay, short numIcons, short firstIcon);
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. //=============================================================================
  63. //        Global variables                                                                 
  64. //-----------------------------------------------------------------------------
  65. ESParamBlock    *gTheParamBlock;
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. //=============================================================================
  82. //        main : Entry point to our code resource.                                                                 
  83. //-----------------------------------------------------------------------------
  84. //        Note :    Extension Shell communicates with us via a message constant,
  85. //                and a pointer to a structure it owns. Our job is to fill in
  86. //                the details in that structure, depending on what it wants us
  87. //                to do.
  88. //-----------------------------------------------------------------------------
  89. void main(short theMsg, ESParamBlock *theParamBlock)
  90. {    long            oldA4;
  91.  
  92.  
  93.  
  94.     // Set up A4 so that we can access our globals
  95. #ifndef powerc
  96.     oldA4 = SetCurrentA4();
  97. #endif
  98.     gTheParamBlock = theParamBlock;
  99.  
  100.  
  101.  
  102.     // Case out on what we have to do
  103.     switch(theMsg) {
  104.         case kInitialiseParamBlock:
  105.              InitialiseParamBlock();
  106.              break;
  107.              
  108.         case kInitialiseAddrsTable:
  109.              InitialiseAddrsTable();
  110.              break;
  111.  
  112.         case kHandleError:
  113.              HandleTheError();
  114.              break;
  115.     
  116.         default:
  117.              ;
  118.     }
  119.  
  120.  
  121.     // Restore A4
  122. #ifndef powerc
  123.     SetA4(oldA4);
  124. #endif
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. //=============================================================================
  142. //        InitialiseParamBlock : Initialises the ParamBlock.                                                                 
  143. //-----------------------------------------------------------------------------
  144. //        Note :    We have three tasks to perform.
  145. //                    • Check to see if we can run
  146. //                    • Set up the icons we want to display
  147. //                    • Set up the code we want installed
  148. //-----------------------------------------------------------------------------
  149. void InitialiseParamBlock(void)
  150. {
  151.  
  152.  
  153.     // Check for System 7. We depend on having System 7, and won't
  154.     // run if we don't have it. If we don't have it, we beep, post
  155.     // an error message, and show our disabled icon(s).
  156.     if (gTheParamBlock->systemVersion < 0x0700)
  157.         {
  158.         // Error details
  159.         gTheParamBlock->beepNow                = true;
  160.         gTheParamBlock->postError            = true;
  161.         gTheParamBlock->errorStringsID        = kErrorStrings;
  162.         gTheParamBlock->errorStringIndex    = kNeedSystemSeven;
  163.  
  164.  
  165.         // Icon details
  166.         SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  167.         }
  168.     
  169.     
  170.     
  171.     // If a shift key, or the mouse button, is down, we don't load either.
  172.     // We don't post an error, but we do show our disabled icon(s) to let
  173.     // the user know they've turned us off.
  174.     else if ((*gTheParamBlock->IsKeyMouseDown)(kShiftKey, true))
  175.         {
  176.         // Icon details
  177.         SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  178.         }
  179.     
  180.     
  181.     
  182.     // Otherwise, we're allowed to run. If we had a Control Panel, we
  183.     // would also check to see if our Control Panel had turned us
  184.     // off (by setting some preference resource).
  185.     else
  186.         {
  187.         // Icon details
  188.         SetUpIcons(kEnabledAnimDelay, kMyNumEnabledIcons, kMyFirstEnabledIcon);
  189.         
  190.         
  191.         // We install one time manager task, and request an address table
  192.         gTheParamBlock->installAddressTable        = true;
  193.         gTheParamBlock->addressTableSelector    = kMacCoughAddressTable;
  194.         gTheParamBlock->numCodeResources        = 1;
  195.  
  196.  
  197.         // Details for a Time Manager Task. We prime it to go off in 60 seconds,
  198.         // since the Time Manager is functioning at boot time (and we are
  199.         // playing the sound via the Notification Manager, which isn't: if
  200.         // our Time Manager task was called several times during boot time,
  201.         // it would stack up a series of coughs, all of which would be played
  202.         // when the Finder starts up...)
  203.         gTheParamBlock->theCodeResources[kTimeTask].resType        = kTimeTaskResType;
  204.         gTheParamBlock->theCodeResources[kTimeTask].resID        = kTimeTaskResID;
  205.         gTheParamBlock->theCodeResources[kTimeTask].codeType    = kTimeManagerTaskType;
  206.         gTheParamBlock->theCodeResources[kTimeTask].theCodeThing.theTimeManagerTask.theDelay = 60000;
  207.         }
  208. }
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224. //=============================================================================
  225. //        InitialiseAddrsTable : Initialise the address table.                                                     
  226. //-----------------------------------------------------------------------------
  227. //        Note :    If we requested an address table, Extension Shell calls us back
  228. //                to allow us to initialise any extensions we've made to it.
  229. //
  230. //                This routine will only be called if we request an address
  231. //                table, and is called after the address table is installed,
  232. //                but before any of the items in gTheParamBlock->theCodeResources
  233. //                are processed (since they might need to access the address
  234. //                table).
  235. //
  236. //                We should initialise the magicNumber and versionNumber fields
  237. //                with constants fixed for this build. One possible number for the
  238. //                magicNumber field would be sizeof() our address table structure.
  239. //-----------------------------------------------------------------------------
  240. void InitialiseAddrsTable(void)
  241. {    MCAddressTable    *theAddressTable;
  242.     Handle            theHnd;
  243.  
  244.  
  245.  
  246.     // Call Gestalt to find our address table
  247.     Gestalt(kMacCoughAddressTable, (long *) &theAddressTable);
  248.  
  249.  
  250.     // Initialise the magic number and version fields
  251.     theAddressTable->magicNumber    = kMyTableMagic;
  252.     theAddressTable->versionNumber    = kMyTableVersion;
  253.  
  254.  
  255.  
  256.     // Initialise our custom values
  257.     theHnd = GetResource('snd ', kTheCoughNoise);
  258.     DetachResource(theHnd);
  259.     theAddressTable->theSound = theHnd;
  260. }
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276. //=============================================================================
  277. //        HandleTheError : Handle any errors                                                             
  278. //-----------------------------------------------------------------------------
  279. //        Note :    This routine is called if an error occurred during the
  280. //                installation of the items in gTheParamBlock->theCodeResources.
  281. //
  282. //                If an error occurs we beep, post an error, and request that
  283. //                as much as possible of our code be uninstalled. We also reset
  284. //                the icon details to show our disabled icons.
  285. //-----------------------------------------------------------------------------
  286. void HandleTheError(void)
  287. {
  288.  
  289.  
  290.     // Decide how we want to handle the error
  291.     gTheParamBlock->removeInstalledCode    = true;
  292.     gTheParamBlock->beepNow                = true;
  293.     gTheParamBlock->postError            = true;
  294.     gTheParamBlock->errorStringsID        = kErrorStrings;
  295.  
  296.  
  297.     // Message to display to the user
  298.     gTheParamBlock->errorStringIndex = kUnknownError;
  299.  
  300.  
  301.     // Icon details
  302.     SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  303. }
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319. //=============================================================================
  320. //        SetUpIcons : Set up our the icon fields in gTheParamBlock.                                                         
  321. //-----------------------------------------------------------------------------
  322. //        Note :    We are passed in the resource ID of the first icon, the number
  323. //                of icons, and a delay for animation. We fill these details
  324. //                in to gTheParamBlock.
  325. //-----------------------------------------------------------------------------
  326. void SetUpIcons(short animDelay, short numIcons, short firstIcon)
  327. {    short        i;
  328.  
  329.  
  330.     gTheParamBlock->animationDelay    = animDelay;
  331.     gTheParamBlock->numIcons        = numIcons;
  332.     for (i = 1; i <= numIcons; i++)
  333.         gTheParamBlock->theIcons[i] = firstIcon + i - 1;
  334. }
  335.